﻿using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    public class CheckTaskController : PageController
    {
        [HttpGet, Route("viewLog"), JwtAuthActionFilter]
        public HttpResponseMessage CheckTask(int code,int page = 1, int limit = 15)
        {
            string sql = "";
            int loginId = GetCrrentIDByToken();
            User curUser = GetUserByLoginID(loginId);
            if(curUser==null)
                return AssistFun.toJson(new { status = 201, tip = "违法登陆者", data = "" });
            //power等于0是普通员工，1是主管，2是管理员  
            if (curUser.power == 1)
                sql = " and task_tb.id = pc_tb.pc_id and pc_tb.plid = user_tb.plid and task_tb.user_id = User_tb.login_id and  User_tb.PLID = " + curUser.PLID + " and Task_tb.code=" + code;
            if(curUser.power==0)
                sql = " and task_tb.id = pc_tb.pc_id and pc_tb.plid = user_tb.plid and task_tb.user_id = User_tb.login_id and Task_tb.code="+code+"  and User_tb.PLID = " + curUser.PLID + "  and User_tb.login_id=" + curUser.login_id ;
            //if(curUser.power==2)
            //    sql = " and task_tb.id = pc_tb.pc_id and pc_tb.plid = user_tb.plid and task_tb.user_id = User_tb.login_id ";

            int totalPage = 0;
            int totalRow = 0;
            string tables = " task_tb,pc_tb,user_tb";
            string fileds = " pc_id as PcId, pc_tb.path as Pc_Name,task_tb.code as Code,user_tb.name as UserName,task_tb.time as time";
            DataTable dt = null;
            try
            {
                SQLCommand.GetDataListByPage(tables, fileds, "task_tb.time desc", sql, limit, page, out totalPage, out totalRow,out dt);
                if (dt == null)
                    return AssistFun.toJson(new { status = 201, tip = "暂时没有日志信息", data = "" });
                else
                {
                    List<CheckTask> result = AssistFun.DataTableToList<CheckTask>(dt);
                    List<StateCheckTask> res = new List<StateCheckTask>();
                    int state = 0;
                    foreach (CheckTask item in result)
                    {
                        int step = GetPcStep(item.PcId);
                        if (item.Code != step && item.Code < step)  
                            state = 1;
                        res.Add(new StateCheckTask(item,state));
                    }
                    return AssistFun.toJson(new {status = 200, tip = "成功", data = new { total = totalRow, rows = res.OrderBy(r=>r.State) } });
                }
            }
            catch (Exception e)
            {
                return AssistFun.toJson(new { status = 201, tip = e.Message, data = "" });
            }
        }

        #region pod详情
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns>pod详情 </returns>
        [HttpGet, Route("pods")]
        public HttpResponseMessage GetPodInfo(int page = 1, int limit = 15)
        {
            int loginId = GetCrrentIDByToken();
            User curUser = GetUserByLoginID(loginId);
            if (curUser == null)
                return AssistFun.toJson(new { status = 201, tip = "违法登陆者", data = "" });
            string sql = "";
            if (curUser.power == 0)
                sql = " and User_tb.login_id=" + curUser.login_id;
            else
                sql = " and user_id in (select login_id from User_tb where PLID="+curUser.PLID+")";
            int totalPage = 0;
            int totalRow = 0;
            string tables = " Pod_tb left join User_tb on user_id=login_id join PC_tb on pc_id = Pod_tb.order_id ";
            string fileds = "  PC_tb.path as Pc_Name,pod as Pod,User_tb.name as UserName,time,Pod_tb.state as State";

            DataTable dt = null;
            SQLCommand.GetDataListByPage(tables, fileds, "Pod_tb.state asc", sql, limit, page, out totalPage, out totalRow, out dt);
            if (dt == null) return AssistFun.toJson(new { status = 201, tip = "没有相关的pod数据" });
            else
            {
                List<PodCheckTask> podinfo = AssistFun.DataTableToList<PodCheckTask>(dt);
                return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = totalRow, rows = podinfo.OrderBy(p=>p.State) } });
            }

        }

        #endregion
    }
}
